package org.codeblue.support.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.Map.Entry;

public class Maps {
    public static Map newMap() {
        return new HashMap();
    }


    public static Boolean isEmpty(Map map) {
        return null == map || map.isEmpty();
    }

    public static Map<?,?> append(Map map, Object... keyAndValues) {
        Map result = new HashMap();
        result.putAll(map);
        result.putAll(toMap(keyAndValues));
        return result;
    }

    public static Map<String, Object> toStringObjectMap(Object... items) {
        return toMap(items);
    }

    public static String toString(Map map){
       return toJSONObject(map).toJSONString();
    }

    public static JSONObject toJSONObject(Map map){
        return  new JSONObject(map);
    }

    public static Map toMap(Object... items) {
        if (null != items && items.length % 2 != 0) {
            throw new IllegalArgumentException("Count of items must be even !!!");// 个数必须为偶数,抛出异常
        } else {
            Map map = new HashMap();
            for (int i = 0; null != items && i < items.length; i = i + 2) {
                map.put(items[i], items[i + 1]);
            }
            return map;
        }
    }

    public static <K, V> Map<K, V> noNull(Map<K, V> map) {
        return null == map ? new HashMap<K, V>() : map;
    }

    public static Map<String, String> sortMap(final Map<String, String> map) {
        TreeMap<String, String> treeMap = new TreeMap<String, String>(new Comparator<String>() {
            public int compare(String key1, String key2) {
                return (key1 + map.get(key1)).compareTo(key2 + map.get(key2));
            }
        });
        treeMap.putAll(map);
        return treeMap;
    }

    public static <K, V> Map<K, V> select(Map<K, V> map, K... keys) {
        Map<K, V> result = new HashMap<K, V>();
        for (int i = 0; null != keys && i < keys.length; i++) {
            result.put(keys[i], map.get(keys[i]));
        }
        return result;
    }

    public static void put(Map map, String key, Object value) {
        map.put(key, value);
    }

    public static <T> Map<T, T> sameKeyValueMap(Collection<T> values) {
        Map<T, T> map = new HashMap<T, T>();
        for (T value : values) {
            map.put(value, value);
        }
        return map;
    }

    public static Map remove(Map input, String... keysToRemove) {
        for (String key : keysToRemove) {
            input.remove(key);
        }
        return input;
    }

    public static Map merge(Map... maps) {
        Map result = new HashMap();
        for (Map map : maps) {
            if (null != map) {
                result.putAll(map);
            }
        }
        return result;
    }

    public static boolean containsKey(Map map, String key) {
        return null != map && map.containsKey(key);
    }

    /**
     * map 反转
     */
    public static Map reverse(Map map) {
        if (null == map) {
            return null;
        } else {
            Map result = new HashMap();
            Set entrySet = map.entrySet();
            for (Object each : entrySet) {
                Entry entry = (Entry) each;
                Object key = entry.getKey();
                Object value = entry.getValue();
                if (null != key && null != value) {
                    result.put(value, key);
                }
            }
            return result;
        }
    }
}